//package cn.topevery.garden.wapi.config;
//
//
//import cn.topevery.garden.wapi.service.AsyncService;
//import hw.topevery.basis.entity.po.SysLog;
//import hw.topevery.basis.entity.po.SysUser;
//import hw.topevery.basis.runtime.UserRuntime;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.time.LocalDateTime;
//import java.util.Arrays;
//import java.util.Objects;
//
///**
// * 切面日志配置
// */
//@Aspect
//@Component
//@Slf4j
//public class LogAsPect {
//
//    @Autowired
//    private AsyncService asyncService;
//
//    /**
//     * 表示匹配带有自定义注解的方法
//     */
//    @Pointcut("execution(* cn.topevery.garden.wapi.controller.*.*(..))")
//    public void pointcut() {
//    }
//
//    @Around("pointcut()")
//    public Object around(ProceedingJoinPoint point) {
//        Object result = null;
//        long beginTime = System.currentTimeMillis();
//
//        try {
//            result = point.proceed();
//            long endTime = System.currentTimeMillis();
//            insertLog(point, endTime - beginTime);
//        } catch (Throwable e) {
//            // TODO Auto-generated catch block
//        }
//        return result;
//    }
//
//    private void insertLog(ProceedingJoinPoint point, long time) {
//        MethodSignature signature = (MethodSignature) point.getSignature();
//        Method method = signature.getMethod();
//        SysLog sysLog = new SysLog();
//
//        String methodDesc = "";
//        MethodLog userAction = method.getAnnotation(MethodLog.class);
//        if (userAction != null) {
//            // 注解上的描述
//            methodDesc = "操作模块:" + userAction.operModul() + ",操作说明:" + userAction.operDesc();
//        }
//        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
//        //请求的IP
//        String ip = request.getRemoteAddr();
//
//        // 请求的类名
//        String className = point.getTarget().getClass().getName();
//        // 请求的方法名
//        String methodName = signature.getName();
//
//        if (methodName.equals("login")) {
//            sysLog.type = 1;
//        } else {
//            sysLog.type = 2;
//        }
//
//        // 请求的方法参数值
//        String args = Arrays.toString(point.getArgs());
//
//        SysUser sysUser = UserRuntime.getSysUser();
//        assert sysUser != null;
//        sysLog.userId = sysUser.userId;
//        sysLog.userName = sysUser.userName;
//        sysLog.createTime = LocalDateTime.now();
//        sysLog.updateTime = LocalDateTime.now();
//        sysLog.dbStatus = false;
//        sysLog.visitIp = ip;
//        sysLog.urlAddress = className + "." + methodName;
//        sysLog.logTime = LocalDateTime.now();
//        sysLog.logContent = methodDesc + ",参数:" + args + ",执行时间：" + time;
//        try {
//            asyncService.saveOpLog(sysLog.userId, sysLog);
//        }catch (Exception ex){
//            log.error(ex.getMessage(),ex);
//            ex.printStackTrace();
//        }
//    }
//
//}